package com.coffee.ws;

import com.coffee.core.CoffeeResponseEnum;
import com.coffee.core.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;


/**
 * @author: songkui
 * @date: 2022/3/15 14:56
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CoffeeWebsocketResponse<T> implements Serializable {

    private int state;
    private int opcode;
    private int cmd; // websocket 命令号
    private String message;
    private T data;


    public CoffeeWebsocketResponse(int cmd, T data, ResponseCode responseCode){
        this.cmd = cmd;
        this.data = data;
        this.state = responseCode.code();
        this.opcode = responseCode.opcode();
        this.message = responseCode.message();
    }


    public static <T> CoffeeWebsocketResponse<T> response(int cmd, T data, ResponseCode responseCode){
        return new CoffeeWebsocketResponse<>(cmd, data, responseCode);
    }

    public static <T> CoffeeWebsocketResponse<T> response(T data, ResponseCode responseCode){
        return response(0, data, responseCode);
    }

    public static <T> CoffeeWebsocketResponse<T> responseSuccess(int cmd, T data){
        return response(cmd, data, CoffeeResponseEnum.Success);
    }

    public static <T> CoffeeWebsocketResponse<T> responseSuccess(T data){
        return response(0, data, CoffeeResponseEnum.Success);
    }

    public static <T> CoffeeWebsocketResponse<T> responseFailure(ResponseCode responseCode){
        return response(0, null, responseCode);
    }

    public static <T> CoffeeWebsocketResponse<T> responseFailure(int cmd, ResponseCode responseCode){
        return response(cmd, null, responseCode);
    }



}